package com.qianchang.microfinance.setting;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.HelpCenterBean;
import com.qianchang.microfinance.bean.HelpCenterRootBean;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HelpCenterItemActivity extends BaseActivity {

    private static final String TAG = "HelpCenterItemActivity";
    @BindView(R.id.listview)
    ListView listview;
    private List<HelpCenterBean> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_center_item);
        ButterKnife.bind(this);
        initTitle();
        getList();
    }

    private void initTitle() {
        setTitleName(getIntent().getStringExtra("title"));
    }


    private void getList() {
//        4.8 查询帮助中心列表 or 详情
//        接口URL：
//
//            请求方式:
//                    post
//        参数输入：
//        序号	参数名	类型	长度	是否必须	描述
//        1	id	String	25	N	主键id
//        2	title	String	25	N	栏目标题
//        3	noticeType	String	25	N	模块类型 0帮助中心 1关于我们
//        4	nodeId	String	25	N	节点ID
//        5	status	String	25	N	状态 0：启用 1： 停用
//        6	isTop	String	25	N	是否置顶 1是 2否
//        7	isRelease	String	25	N	是否发布: 1已发布，2未发布
//        8	createId	String	25	N	创建人
//        9	userName	String	25	N	用户名
//        10	sendSide	String	25	N	发送端
//        11	startTime	String	25	N	开始时间
//        12	endTime	String	25	N	结束时间
//        13	column	String	25	N	查二级菜单
//        14	sysUserRole	String	25	N	用户角色

        String nodeId = getIntent().getStringExtra("id");
        DMLog.d("nodeId = " + nodeId);
        show();
        HttpParams params = new HttpParams();
        params.put("moduleType", 0);
        params.put("nodeId", nodeId);
        params.put("sendSide", "SEND_SIDE_2");
//        moduleType = 0;    nodeId = 00101501730864944227;    sendSide = "SEND_SIDE_2";}第二级的接口和参数
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.HELP_CENTER_LIST, params, new RequestCallBack<HelpCenterRootBean>() {//UserLoanInfo
            @Override
            public void onSuccess(JsonResult<HelpCenterRootBean> response) {
                // {"code":"000000","data":{"hasNextPage":false,"hasPreviousPage":false,"list":[{"content":"222222222","createId":"1","createTime":"2017-07-22 10:57:52","id":"00101500692336152102","isRelease":"1","isTop":"0","keyWord":"无","moduleType":"0","nodeId":"00101495001620053170","sendSide":"PC后端；PC前端；移动客户端；移动办公端","status":"0","title":"体验金标00_1","userName":"admin"},{"content":"111111","createId":"1","createTime":"2017-07-22 10:57:31","id":"00101500692315623101","isRelease":"1","isTop":"0","keyWord":"无","moduleType":"0","nodeId":"00101495001620053170","sendSide":"PC后端；移动客户端","status":"0","title":"1111114","userName":"admin"}],"pageIndex":1,"pageSize":10,"pageTotal":1,"recordCount":2},"description":null}
                dismiss();
                DMLog.i(TAG, response.toString());
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    mList = response.getObject().getList();
                    if (mList != null && !mList.isEmpty()) {
                        listview.setAdapter(new MyAdapter(mList, HelpCenterItemActivity.this));
                    } else {
                        ToastUtil.getInstant().show(HelpCenterItemActivity.this, getString(R.string.has_no_data));
                    }
                } else {
                    ToastUtil.getInstant().show(HelpCenterItemActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                DMLog.i(TAG, "onFailure: " + msg);
                ToastUtil.getInstant().show(HelpCenterItemActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                dismiss();
            }
        });

    }

    private static class MyAdapter extends BaseAdapter {
        private List<HelpCenterBean> mlist;
        private Context context;

        public MyAdapter(List<HelpCenterBean> mlist, Context context) {
            this.mlist = mlist;
            this.context = context;
        }

        @Override
        public int getCount() {
            return mlist.size();
        }

        @Override
        public HelpCenterBean getItem(int position) {
            return mlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final HelpCenterBean entity = getItem(position);
            ViewHolder vh = null;
            if (convertView == null) {
                vh = new ViewHolder();
                convertView = View.inflate(context, R.layout.help_center_item, null);
                vh.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
                vh.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
                vh.cb = (CheckBox) convertView.findViewById(R.id.cb);
                vh.llContent = convertView.findViewById(R.id.ll_content);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            vh.tvTitle.setText(getItem(position).getTitle());
            vh.tvContent.setText(getItem(position).getContent());
            if (entity.getVisible()) {
                vh.llContent.setVisibility(View.VISIBLE);
            } else {
                vh.llContent.setVisibility(View.GONE);
            }
            final ViewHolder finalVh = vh;
            vh.tvTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    entity.setVisible(!entity.getVisible());
                    finalVh.cb.setChecked(entity.getVisible());
                    notifyDataSetChanged();
                }
            });

            return convertView;
        }
    }

    static class ViewHolder {
        public TextView tvTitle;
        public TextView tvContent;
        public CheckBox cb;
        public View llContent;
    }
}
